<?php

/* 确认支付 */




// 目标URL
$url = 'Https://Pay.Heepay.com/WithholdAuthPay/ConfirmPay.aspx';


// 设置自己的私钥
$private_key_str = 
"-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC5XkR5gTRgWLAUeyCvnQZA4pdZ6ayV8R+//KzQjDW18SEaFb/g+9b4jX6tyWniEXWYhXrHXyTbI6ZvykRpD2HfMo1Bvb1OKIVZYKG9chJmcekGKufbmQ4Pxuoy72I8Qhi8zmO29pROE7nM7EHgh1aYtcpJIBmq9Zuc8BddIk6t4Cxo6NMtHSbgzXCcqw67zCRDyf8mLZIc+o5ObzNZqbqAwD7bRUctCbYlUumXNJYBzbQ3aNQxZow1GDw23zbqqPgRVI3iXyNX5FWipTMUgGDpo6wDF3hOT1Z0xRdtBfEkdT6UyruRKGbbWiDd9roa75W4mlmvM0y7g7ZhuVkoaJn7AgMBAAECggEAFGeKBg3w5AIfL8H18R7Jx26p1l3gf4jRQ+Leav5kEyj9yc9HFc7i9O0uWpbnLtlZJnba8wNIHU8pTPjo2t+0mEsKPYhTRD01oIcAhsf9uoUjb2hdqVCJubbdpMmw+I0WCAJ3+3XdEVMeiq4e+kYrhBfOson0CVuLy5SqHbhfDMiF6SzRiTFMAELos7SwfjXZ0VAclGP7do/XWyUp2b2l9Z0nnaeKU2ecNBZlDXKcpsZ2Omz8HIXyJpeKooN63DSkjobPm6+/saNAL5118ZC8SxnvbZ61gsEk8/feHoYr58g6zgllLEb5uR7DJjmxrCF/bMqGv2OQ35s0cnPwUds/cQKBgQD7JACTjg2D/mvBSeZxsmOiLqJIoxeUOlH7OliPe6uF9XAJat2LJCpN5UPbUqgBC280SvzhJG103WeZIBNYiRqxC0qQUw1qUkpavJ1TkYw7lO5m/9b5TebQ05qTiBWd1DmJYbXfPQRjwjAG2/MkGIvdnZ43NhqnQeNXUHtr7T1RrQKBgQC89HgDymVmgHo1Ffc1z1cMOdnJzIXr6O3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6hIKZ3JJBVkGnuVA7tftVCRCsqCvgwpL6R/QIDAQAB
-----END PUBLIC KEY-----";


//请求参数

//支付订单创建成功的标记（短信接口返回）
$hy_token_id='';
$verify_code='';



$encrypt_data_string =
"hy_token_id=$hy_token_id&verify_code=$verify_code&version=1";
echo '待加密数据'.$encrypt_data_string;
echo "<br/><hr>";

$sign_string = 
"hy_token_id=$hy_token_id&verify_code=$verify_code&version=1";
echo '待签名数据'.$sign_string;
echo "<br/><hr>";

/* // 使用对方公钥加密encrypt_data_string
$partner_public_key = openssl_pkey_get_public($partner_public_key_str);
openssl_public_encrypt($encrypt_data_string, $encrypted, $partner_public_key);
$encrypt_data = base64_encode($encrypted); */



// 分段加密



//获取密钥
$publicKey = $partner_public_key_str ;


// 要加密的数据
$data = $encrypt_data_string;
// 定义每个分段的大小
$chunkSize = 117; // 2048-bit key can encrypt 117 bytes
// 分段加密
$encryptedData = '';
for ($i = 0; $i < strlen($data); $i += $chunkSize) {
    $chunk = substr($data, $i, $chunkSize);
    openssl_public_encrypt($chunk, $encryptedChunk, $publicKey);
    $encryptedData .= $encryptedChunk;
}
// 将分段加密后的数据进行处理（例如Base64编码）并发送给接收方
$encrypt_data = base64_encode($encryptedData);
//输出加密后数据
echo '加密数据'.$encrypt_data;
echo "<br/><hr>";



// 对签名字符串进行自己的RSA签名
$private_key = openssl_pkey_get_private($private_key_str);
openssl_sign($sign_string, $signature, $private_key, OPENSSL_ALGO_SHA1);
$sign = base64_encode($signature);
//输出签名后数据
echo '签名数据'.$sign;
echo "<br/><hr>";


// 要发送的数据
$data = array(
    'agent_id' => '1664502',
    'sign' => $sign,
    'encrypt_data' => $encrypt_data,
);

// 创建HTTP头部
$options = array(
    'http' => array(
        'header' => "Content-type: application/x-www-form-urlencoded\r\n",
        'method' => 'POST',
        'content' => http_build_query($data),
    ),
);

// 创建上下文流
$context = stream_context_create($options);

// 发送POST请求并获取响应
$response = file_get_contents($url, false, $context);

// 处理响应
if ($response) {
    echo  $response;
} else {
    echo '未收到响应。';
}


?>